package com.mocne.Common;

import jxl.read.biff.BiffException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import redis.clients.jedis.Jedis;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

public class Excel2Dataprovider {

//    public Workbook workbook;
    public Sheet sheet;
    public Cell cell;
    int rows;
    int columns;
    public String fileName;
    public String caseName;
    public ArrayList<String> arrkey = new ArrayList<>();
    String sourceFile;
    Jedis jedis = new Jedis("localhost");

    /**
     * @param fileName   excel文件名
     * @param caseName   sheet名
     */
    public Excel2Dataprovider(String fileName, String caseName) {
        super();
        this.fileName = fileName;
        this.caseName = caseName;

    }

    /**
     * 获得excel表中的数据
     */
    @SuppressWarnings("unchecked")
    public Object[][]  getExcelData() throws BiffException, IOException {

        Workbook workbook = null;

        if(fileName != null){
            String ext = fileName.substring(fileName.lastIndexOf("."));
            try {
                InputStream is = new FileInputStream(fileName);
                if(".xls".equals(ext)){
                    workbook = new HSSFWorkbook(is);
                }else if(".xlsx".equals(ext)){
                    workbook = new XSSFWorkbook(is);
                }else{
                    workbook=null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//        workbook = Workbook.getWorkbook(new File(getPath()));
        sheet = workbook.getSheet(caseName);
        rows = sheet.getPhysicalNumberOfRows();

        if (rows <= 1) {
            System.out.println("excel中没有数据");
            return new HashMap[1][1];
        }
        // 为了返回值是Object[][],定义一个多行单列的二维数组
        HashMap<String, String>[][] arrmap = new HashMap[rows - 1][1];
        // 对数组中所有元素hashmap进行初始化
        for (int i = 0; i < rows - 1; i++) {
            arrmap[i][0] = new HashMap<>();
        }

        columns = sheet.getRow(0).getLastCellNum();

        // 获得首行的列名，作为hashmap的key值
        for (int c = 0; c < columns; c++) {
//            String cellvalue = sheet.getCell(c, 0).getContents();
            String cellvalue = "";
            if (sheet.getRow(0).getCell(c) != null) {
                cellvalue = sheet.getRow(0).getCell(c).toString();
            }
            arrkey.add(cellvalue);
        }
        // 遍历所有的单元格的值添加到hashmap中
        for (int r = 1; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                String cellvalue = "";
                if (sheet.getRow(r).getCell(c) != null) {
                    cellvalue = sheet.getRow(r).getCell(c).toString();
                }
                arrmap[r - 1][0].put(arrkey.get(c), cellvalue);
            }
        }
        return arrmap;
    }

    /**
     * 获得excel文件的路径
     * @return
     * @throws IOException
     */
    public String getPath() throws IOException {

        sourceFile = jedis.get("dpPath") + jedis.get("dpFile");
        return sourceFile;
    }

}
